/*
 * Copyright 2010 Okan Kahraman
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package tr.org.tx.vmot.servlet;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import com.vaadin.terminal.gwt.server.GAEApplicationServlet;

/**
 * @author Okan Kahraman
 *
 */
public class SpringIntegrationServlet extends GAEApplicationServlet {

	
	private static final long serialVersionUID = -5299743553586030101L;
	
	private transient WebApplicationContext webApplicationContext;

	private transient LocaleResolver localeResolver;

	@Override
	public void init(ServletConfig servletConfig) throws ServletException {
		
		super.init(servletConfig);
		
		webApplicationContext = WebApplicationContextUtils
				.getWebApplicationContext(servletConfig.getServletContext());

		initLocaleResolver(webApplicationContext);
	}
	
	private void initLocaleResolver(ApplicationContext context) {
		try {
			localeResolver = context.getBean(
					DispatcherServlet.LOCALE_RESOLVER_BEAN_NAME,
					LocaleResolver.class);
		} catch (NoSuchBeanDefinitionException e) {
			localeResolver = new SessionLocaleResolver();
		}
	}

	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		final Locale locale = localeResolver.resolveLocale(request);
		LocaleContextHolder.setLocale(locale);
		ServletRequestAttributes requestAttributes = new ServletRequestAttributes(
				request);
		RequestContextHolder.setRequestAttributes(requestAttributes);
		try {
			super.service(new HttpServletRequestWrapper(request) {
				@Override
				public Locale getLocale() {
					return locale;
				}
			}, response);
		} finally {
			if (!locale.equals(LocaleContextHolder.getLocale())) {
				localeResolver.setLocale(request, response, LocaleContextHolder
						.getLocale());
			}
			LocaleContextHolder.resetLocaleContext();
			RequestContextHolder.resetRequestAttributes();
		}
	}	
	

}
